/**
  *@file    hh_msg.c
  *@brief   frame message management
  *@anthor  zxf 2020-10-25
  *@version V1.0.0
  *@note    none
  */
#include "hh_msg.h"
#include "hh_format.h"

/* Private define ---------------------------------------*/

#define MSG_SIZE                       40

/* Private macro ----------------------------------------*/
/* Private typedef --------------------------------------*/

typedef struct
{
	u8  total;
	u8  in;
	u8  out;
	u32 msg[MSG_SIZE];
}msgModel_t;

/* Private variables ------------------------------------*/
/* Private function prototypes --------------------------*/
/* Interface variables ----------------------------------*/

msgModel_t g_MsgData;


/* Interface functions ----------------------------------*/

void Message_Init(void)
{
	hh_memset(&g_MsgData, 0x00, sizeof(msgModel_t));
}

//post message to queue
void Message_Post(u32 msg)
{
	msgModel_t *q = &g_MsgData;
	if(q->total >= MSG_SIZE)
	{
		//zprint("msg full!\r\n");
		return;
	}

	q->msg[q->in] = msg;
	q->total++;
	q->in++;
	if(q->in >= MSG_SIZE)
		q->in = 0;
}

//get message from queue
u32  Message_Read(void)
{
	msgModel_t *q = &g_MsgData;
	u32 msg;
	
	if(q->total == 0)
		return HHMSG_INVALID;

	msg = q->msg[q->out];
	q->total--;
	if(q->total == 0)
	{
		q->in = 0;
		q->out = 0;
	}
	else
	{
		q->out++;
		if(q->out >= MSG_SIZE)
			q->out = 0;
	}
    
    return msg;
}

/* Private functions ------------------------------------*/



